package khs.java_file.main.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.xml.bind.DatatypeConverter;

/**
 * Hello world!
 *
 */
public class App 
{
    private static BufferedReader bufferedReader;
    private static String APPLICATION_PATH = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath();
    
    public static void main( String[] args ) throws IOException, ClassNotFoundException
    {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        String input_mode;
        
        do{
            fnPrintMode();
            input_mode = bufferedReader.readLine();
            
            switch(input_mode){
            case "1":
                fnBinaryMode();
                break;
            case "2":
                fnTextMode();
                break;
            case "3":
                fnSerializeMode();
                break;
            }
        }while(!input_mode.equals("q"));
        
        bufferedReader.close();
    }
    
    public static void fnPrintMode()
    {
        System.out.println("1. binary");
        System.out.println("2. text");
        System.out.println("3. serialize");
        System.out.println("q. quit");
    }
    
    public static void fnSerializeMode() throws IOException, ClassNotFoundException
    {
        SerializeSample serializeSample = new SerializeSample();
        serializeSample.setStringValue1("stringValue");
        serializeSample.setIntegerValue1(1);
        
        fnSerializeWrite(serializeSample);
        SerializeSample readData = fnSerializeRead();
        System.out.println(readData);
    }
    
    public static void fnSerializeWrite(SerializeSample serializeData) throws IOException
    {
        FileOutputStream fileOutputStream = new FileOutputStream(APPLICATION_PATH + File.separator + "serialize.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(serializeData);
        objectOutputStream.flush();
        objectOutputStream.close();
        fileOutputStream.close();
    }
    
    public static SerializeSample fnSerializeRead() throws IOException, ClassNotFoundException
    {
        FileInputStream fileInputStream = new FileInputStream(APPLICATION_PATH + File.separator + "serialize.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        SerializeSample rtn = (SerializeSample)objectInputStream.readObject();
        objectInputStream.close();
        fileInputStream.close();
        return rtn;
    }
    
    public static void fnTextMode() throws IOException
    {
        System.out.print("input data: ");
        String input_data = bufferedReader.readLine();
        
        fnTextWrite(input_data);
        String read_data = fnTextRead();
        
        System.out.println(read_data);
    }
    
    public static void fnTextWrite(String input_data) throws IOException
    {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(APPLICATION_PATH + File.separator + "text.txt"));
        
        bufferedWriter.write(input_data);
        bufferedWriter.flush();
        
        bufferedWriter.close();
    }
    
    public static String fnTextRead() throws IOException
    {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(APPLICATION_PATH + File.separator + "text.txt"));
        
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while((line = bufferedReader.readLine()) != null){
            stringBuilder.append(line);
        }
        
        bufferedReader.close();
        
        return stringBuilder.toString();
    }
    
    public static void fnBinaryMode() throws IOException
    {
        byte[] data = {1, 2};
        fnBinaryWrite(data);
        byte[] read = fnBinaryRead();
        
        System.out.println(DatatypeConverter.printHexBinary(read));
    }
    
    public static void fnBinaryWrite(byte[] data) throws IOException
    {
        FileOutputStream fileOutputStream = new FileOutputStream(APPLICATION_PATH + File.separator + "binary.txt");
        fileOutputStream.write(data);
        fileOutputStream.close();
    }
    
    public static byte[] fnBinaryRead() throws IOException
    {
        InputStream inputStream = new FileInputStream(APPLICATION_PATH + File.separator + "binary.txt");
        int numberBytes = inputStream.available();
        byte rtn[] = new byte[numberBytes];
        
        inputStream.read(rtn);
        inputStream.close();
        
        return rtn;
    }
}
